#!/usr/bin/env python

import pypvm
import sys
import os
from signal import SIGTERM
from sys import stdout
from time import sleep

try:

# ============================ test mytid () ==================================
    my_tid = pypvm.mytid ()
    print "MASTER: my tid = %s" % my_tid

# ============================ test get/setopt () =============================
    print "MASTER: testing get/setopt ()"
    val = pypvm.setopt (pypvm.opt['Route'], pypvm.getopt (pypvm.opt['Route']))
    print "PvmRoute =", val

# ============================ test catchout () ===============================
    print "MASTER: setting catchout to stdout..."
    pypvm.catchout (stdout)

# ============================ test delhosts () ===============================
# print "MASTER: removing host hopper..."
# pypvm.delhosts (["hopper"])

# ============================ test addhosts () ===============================
# print "MASTER: adding host hopper..."
# pypvm.addhosts (["hopper"])

# ============================ test spawn () ==================================
    print "MASTER: spawning 2 tasks (slave_test.py)..."
    the_file = os.getcwd() + "/slave_test.py"
    print the_file
    tids = pypvm.spawn (the_file, [], pypvm.spawnOpts['TaskDefault'], "", 2)
    print "MASTER: spawned task's tid = %s" % tids

# ============================ test notify () =================================
    print "MASTER: asking for notification on child exits..."
    pypvm.notify (pypvm.notifyDict['TaskExit'], 2, tids, 1)

# ============================ test initsend () ===============================
    print "MASTER: initializing message buffer to PvmDataDefault..."
    pypvm.initsend (pypvm.data['default'])

# ============================ test mkbuf () ==================================
    print "MASTER: testing mkbuf ()..."
    newrbuf = pypvm.mkbuf (pypvm.data['default'])
    newsbuf = pypvm.mkbuf (pypvm.data['default'])

# ============================ test get[rs]buf () =============================
    print "MASTER: testing get[rs]buf ()..."
    oldrbuf = pypvm.getrbuf ()
    oldsbuf = pypvm.getsbuf ()

# ============================ test set[rs]buf () =============================
    print "MASTER: testing set[rs]buf ()..."
    pypvm.setrbuf (newrbuf)
    pypvm.setsbuf (newsbuf)

# ============================ test freebuf () ================================
    print "MASTER: testing freebuf ()..."
    pypvm.freebuf (oldrbuf)
    pypvm.freebuf (oldsbuf)

# ============================ test pk () =====================================
    data = {-1: [1, (2, 3, 4), 4]}
    print ("MASTER: packing %s..." % data)
    pypvm.pk (data)

# ============================ test send () ===================================
    for recipient in tids:
        print ("MASTER: sending data to tid %s..." % recipient)
        pypvm.send (recipient, 1)

# ============================ test mcast () ==================================
    print "MASTER: initializing message buffer to PvmDataDefault..."
    pypvm.initsend (pypvm.data['default'])

    data = ['1', '2', '3']
    print ("MASTER: packing %s using pkbyte ()..." % data)
    pypvm.pkbyte (data, 1)

    #data = [1.0123456789, -2, 3.14]
    #print ("MASTER: packing %s using pkcplx ()..." % data)
    #pypvm.pkcplx (data, 1)

    #data = [1.0123456789, -2, 3.14]
    #print ("MASTER: packing %s using pkdcplx ()..." % data)
    #pypvm.pkdcplx (data, 1)

    data = [1.0123456789, -2, 3.14]
    print ("MASTER: packing %s using pkdouble ()..." % data)
    pypvm.pkdouble (data, 1)

    data = [1.0123456789, -2, 3.14]
    print ("MASTER: packing %s using pkfloat ()..." % data)
    pypvm.pkfloat (data, 1)

    data = [1, -2, 3]
    print ("MASTER: packing %s using pkint ()..." % data)
    pypvm.pkint (data, 1)

    data = [1, -2, 3]
    print ("MASTER: packing %s using pklong ()..." % data)
    pypvm.pklong (data, 1)

    data = [1, -2, 3]
    print ("MASTER: packing %s using pkshort ()..." % data)
    pypvm.pkshort (data, 1)

    pypvm.mcast (tids, 2)

# ============================ test config () =================================
    print "MASTER: results from config:", pypvm.config (1)

# ============================ test tasks () ==================================
    print "MASTER: results from tasks:", pypvm.tasks (0)

# ============================ test tidtohost () ==============================
    print "MASTER: results from tidtohost:", pypvm.tidtohost (my_tid)

# ============================ test pypvm exception ===========================
    try:
        pypvm.send (-1, 0)
        print "MASTER: deliberate error didn't throw and exception (bad!)"
    except:
        print "MASTER: deliberate send exception caught"

# ============================ sleep ==========================================
    print "MASTER: sleeping for 2 seconds (so slaves can catch up)..."
    sleep (2)

# ============================ test sendsig () ================================
    print "MASTER: sending SIGTERM to children..."
    for tid in tids:
        pypvm.sendsig (tid, SIGTERM)

# ============================ test nrecv () ==================================
    print "MASTER: calling nrecv ()..."
    pypvm.nrecv (tids[0], 1)

# ============================ test trecv () ==================================
# NOTE: this also serves to sleep for 10 seconds, since no message is coming.
    print "MASTER: calling trecv ()..."
    pypvm.trecv (10,tids[0],1)

# ============================ test kill () ===================================
    print "MASTER: killing slaves..."
    for tid in tids:
        pypvm.kill (tid)

# ============================ receive exit notification ======================
# NOTE: This is in conjunction with the notify test above.
    for tid in tids:
        pypvm.recv (-1, 2)
        print "MASTER: received an exit notification..."

# ============================ test exit () ===================================
    print "MASTER: exiting pvm..."
    pypvm.exit ()

except:
    print "A pypvm error occured!"
    a =  sys.exc_info()
    print str(a[0])
    print a[1]
    #print sys.exc_info()
